package org.payeco.risk.service.impl;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.payeco.risk.common.DictionUtil;
import org.payeco.risk.dao.PayecoRiskRuleRepository;
import org.payeco.risk.entity.PayecoRiskRule;
import org.payeco.risk.service.PayecoRuleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.base.Strings;
import com.google.common.collect.Sets;

/**
 * @author yps 实现riskrule的业务查询
 */
@Service
public class PayecoRuleServiceImpl implements PayecoRuleService {
	private final Logger log = LoggerFactory.getLogger(this.getClass());
	@Autowired
	PayecoRiskRuleRepository ruleRepository;

	/**
	 * // String sql = "select r from DnaRiskrule r where 1=1 " // +
	 * "and state = 'State.Enabled' " // +
	 * "and checkmode = 'CheckMode.Forward' " // + //
	 * String.format("and sysmerchantno in ('Generic','%s','%s','%s','%s') ", //
	 * sysMer, extra1, extra2, extra3) // + //
	 * "and ((action not in ('Action.Sms','Action.SMS','Action.Block')) or (action = 'Action.Block' and GROUPNAME = 'Quota.Amount')) "
	 * // + "order by priority";
	 */
	@Override
	public List<PayecoRiskRule> findRiskruleListFor3rd(String sysMerchantNo) {
		try {

			String sysMer = Strings.isNullOrEmpty(sysMerchantNo) ? "Generic"
					: sysMerchantNo;
			String extra1 = "1".equals(DictionUtil.getValue("AuthorizedPay."
					+ sysMerchantNo)) ? "AuthorizedPay" : "None";
			String extra2 = "1".equals(DictionUtil.getValue("IntelligentTV."
					+ sysMerchantNo)) ? "IntelligentTV" : "None";
			String extra3 = "1".equals(DictionUtil.getValue("RiskPack12580."
					+ sysMerchantNo)) ? "RiskPack12580" : "None";
			Set<String> merchantGroups = Sets.newHashSet("Generic", sysMer,
					extra1, extra2, extra3);
			Set<String> inActions = Sets.newHashSet("Action.BlockNeedPreCheck");
			Set<String> noAtions = Sets.newHashSet("Action.Sms", "Action.SMS",
					"Action.Block");
			return ruleRepository.findRiskruleListWithAtionType(merchantGroups,
					"CheckMode.Forward", "State.Enabled", inActions, noAtions);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return new LinkedList<PayecoRiskRule>();
	}

	/**
	 * String sql = "select r from DnaRiskrule r where 1=1 " +
	 * (Strings.isNullOrEmpty(r.getState()) ? "" : "and state = '" +
	 * r.getState() + "' ") + (Strings.isNullOrEmpty(r.getCheckmode()) ?"" :
	 * "and checkmode = '" + r.getCheckmode() + "' ") +
	 * String.format("and sysmerchantno in ('Generic','%s','%s','%s','%s') ",
	 * sysMer, extra1, extra2, extra3) + "order by priority";
	 */
	@Override
	public List findRiskruleListWithGeneric(PayecoRiskRule r) {

		try {

			String sysMer = Strings.isNullOrEmpty(r.getSysmerchantno()) ? "Generic"
					: r.getSysmerchantno();
			String extra1 = "1".equals(DictionUtil.getValue("AuthorizedPay."
					+ r.getSysmerchantno())) ? "AuthorizedPay" : "None";
			String extra2 = "1".equals(DictionUtil.getValue("IntelligentTV."
					+ r.getSysmerchantno())) ? "IntelligentTV" : "None";
			String extra3 = "1".equals(DictionUtil.getValue("RiskPack12580."
					+ r.getSysmerchantno())) ? "RiskPack12580" : "None";
			Set<String> merchantGroups = Sets.newHashSet("Generic", sysMer,
					extra1, extra2, extra3);

			return ruleRepository.findRiskruleListWithGeneric(merchantGroups,
					r.getCheckmode(), r.getState());

		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return new LinkedList();
	}

	@Override
	public List findRiskruleListWithDisney(PayecoRiskRule r) {

		try {

			String sysMer = Strings.isNullOrEmpty(r.getSysmerchantno()) ? "Generic"
					: r.getSysmerchantno();
			String extra1 = "1".equals(DictionUtil.getValue("AuthorizedPay."
					+ r.getSysmerchantno())) ? "AuthorizedPay" : "None";
			String extra2 = "1".equals(DictionUtil.getValue("IntelligentTV."
					+ r.getSysmerchantno())) ? "IntelligentTV" : "None";
			String extra3 = "1".equals(DictionUtil.getValue("RiskPack12580."
					+ r.getSysmerchantno())) ? "RiskPack12580" : "None";
			Set<String> merchantGroups = Sets.newHashSet("Generic", sysMer,
					extra1, extra2, extra3, "Disney");

			return ruleRepository.findRiskruleListWithGeneric(merchantGroups,
					r.getCheckmode(), r.getState());

		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return new LinkedList();
	}

	@Override
	public List findRiskruleListWithAbroad(PayecoRiskRule r) {

		try {

			String sysMer = Strings.isNullOrEmpty(r.getSysmerchantno()) ? "Generic"
					: r.getSysmerchantno();
			String extra1 = "1".equals(DictionUtil.getValue("AuthorizedPay."
					+ r.getSysmerchantno())) ? "AuthorizedPay" : "None";
			String extra2 = "1".equals(DictionUtil.getValue("IntelligentTV."
					+ r.getSysmerchantno())) ? "IntelligentTV" : "None";
			String extra3 = "1".equals(DictionUtil.getValue("RiskPack12580."
					+ r.getSysmerchantno())) ? "RiskPack12580" : "None";
			Set<String> merchantGroups = Sets.newHashSet("Generic", sysMer,
					extra1, extra2, extra3, "Aborad");

			return ruleRepository.findRiskruleListWithGeneric(merchantGroups,
					r.getCheckmode(), r.getState());

		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return new LinkedList();
	}

	@Override
	public List findRiskruleListWithDF(PayecoRiskRule r, String merchantGrouop) {
		try {
			String sysMer = Strings.isNullOrEmpty(r.getSysmerchantno()) ? "Generic"
					: r.getSysmerchantno();
			Set<String> merchantGroups = Sets
					.newHashSet(sysMer, merchantGrouop);
			return ruleRepository.findRiskruleListWithGeneric(merchantGroups,
					r.getCheckmode(), r.getState());

		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return new LinkedList();
	}

	@Override
	public List findRiskruleListWithGenericRoute(PayecoRiskRule r,
			boolean isReqNoPwd) {

		try {

			String sysMer = Strings.isNullOrEmpty(r.getSysmerchantno()) ? "Generic"
					: r.getSysmerchantno();
			String extra1 = isReqNoPwd ? "MobilePluginNoPass" : "None";
			Set<String> merchantGroups = Sets.newHashSet("Generic", sysMer,
					extra1);

			// String sql = "select r from DnaRiskrule r where 1=1 "
			// + (Strings.isNullOrEmpty(r.getState()) ? "" : "and state = '" +
			// r.getState() + "' ")
			// + (Strings.isNullOrEmpty(r.getCheckmode()) ?"" :
			// "and checkmode = '" + r.getCheckmode() + "' ")
			// + String.format("and sysmerchantno in ('Generic','%s','%s') ",
			// sysMer, extra1)
			// + "and action like 'Route%' "
			// + "order by priority";

			return ruleRepository.findRiskruleListWithGenericAndAction(
					merchantGroups, r.getCheckmode(), r.getState(), "Route");
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return new LinkedList();
	}

	@Override
	public List findRiskruleListWithGeneric(PayecoRiskRule r,
			String merchantGroup) {
		try {

			String sysMer = Strings.isNullOrEmpty(r.getSysmerchantno()) ? "Generic"
					: r.getSysmerchantno();
			String extra1 = "1".equals(DictionUtil.getValue("AuthorizedPay."
					+ r.getSysmerchantno())) ? "AuthorizedPay" : "None";
			String extra2 = "1".equals(DictionUtil.getValue("IntelligentTV."
					+ r.getSysmerchantno())) ? "IntelligentTV" : "None";
			String extra3 = "1".equals(DictionUtil.getValue("RiskPack12580."
					+ r.getSysmerchantno())) ? "RiskPack12580" : "None";
			Set<String> merchantGroups = Sets.newHashSet("Generic", sysMer,
					extra1, extra2, extra3, merchantGroup);

			return ruleRepository.findRiskruleListWithGeneric(merchantGroups,
					r.getCheckmode(), r.getState());

		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return new LinkedList();
	}

	@Override
	public List findRiskruleListWithGroups(PayecoRiskRule r,
			Set<String> merchantGroups) {
		try {
			String sysMer = Strings.isNullOrEmpty(r.getSysmerchantno()) ? "Generic"
					: r.getSysmerchantno();
			String extra1 = "1".equals(DictionUtil.getValue("AuthorizedPay."
					+ r.getSysmerchantno())) ? "AuthorizedPay" : "None";
			String extra2 = "1".equals(DictionUtil.getValue("IntelligentTV."
					+ r.getSysmerchantno())) ? "IntelligentTV" : "None";
			String extra3 = "1".equals(DictionUtil.getValue("RiskPack12580."
					+ r.getSysmerchantno())) ? "RiskPack12580" : "None";
			merchantGroups.addAll(Sets.newHashSet("Generic", sysMer, extra1,
					extra2, extra3));
			return ruleRepository.findRiskruleListWithGeneric(merchantGroups,
					r.getCheckmode(), r.getState());

		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return new LinkedList();
	}

	@Override
	public List findRiskruleListWithoutDeleted(PayecoRiskRule r, int page) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List findRiskRuleAllList() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List findRiskruleListWithGenericApi(PayecoRiskRule rule,
			String merchantGroup) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List selectList(String sql, Integer page) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List findRiskruleListWithGroup(PayecoRiskRule rule,
			String merchantGroup) {
		Set<String> merchantGroups = Sets.newHashSet(merchantGroup);
		return ruleRepository.findRiskruleListWithGeneric(merchantGroups,
				rule.getCheckmode(), rule.getState());
	}

}
